package net.dgg.zz.production.receive.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.iboss.base.util.poi.ExcelUtil;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.common.utils.ValidateUtils;
import net.dgg.zz.production.entity.ProductOrder;
import net.dgg.zz.production.excption.ProductOrderExcption;
import net.dgg.zz.production.nodeOverdue.service.ProductOrderService;
import net.dgg.zz.production.receive.service.ReceiveOrderService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 待接收controller
 * 
 *<p>@Package: net.dgg.zz.production.receive.controller</p>
 *<p>@Title：ReceiveController</p>
 *<p>@description:</p>
 *<p>@author liulei</p>
 *<p>@date 2018年8月20日</p>
 *<p>@Company dgg</p>
 */
@Controller
@RequestMapping("/sc/receives")
public class ReceiveOrderController extends DggBaseController {
	
	
	@Autowired
	private ReceiveOrderService receiveService;
	
	
	@Autowired
	private SessionManager sessionmanager;
	
	@Autowired
	private TranslateUtil translateUtil;
	
	@Autowired
	private DesUtil desUtil;
	@Autowired
    private ProductOrderService productOrderService;
	
    /**
     * 接收订单列表
     * @param model
     * @param status 状态
     * @param timeStatus
     * @return
     */
    @NLog
    @RequestMapping(value = "/wait_receive")
    public String orderListPage(Model model,String status) {
        model.addAttribute("SC_TO_RECEIVE_FPDJS", ScProductConstant.SC_TO_RECEIVE_FPDJS);//分配待接收
        model.addAttribute("SC_TO_RECEIVE_ZDDJS", ScProductConstant.SC_TO_RECEIVE_ZDDJS);//转单待接收
        model.addAttribute("SC_TO_RECEIVE_YJDJS", ScProductConstant.SC_TO_RECEIVE_YJDJS);//移交待接收
        model.addAttribute("SC_TO_RECEIVE_TDDJS", ScProductConstant.SC_TO_RECEIVE_TDDJS);//退回待接收
        model.addAttribute("ALL","all");
        if(net.dgg.zz.common.utils.StringUtils.isEmpty(status)) {
        model.addAttribute("status", "all");}//默认全部
        else {
        model.addAttribute("status",status);}
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "zzsc/receiveOrder/wait_receive";
    }
	
    /**
     * 转单弹窗页面
     * @param model
     * @param status 状态
     * @param timeStatus
     * @return
     */
    @NLog
    @RequestMapping(value = "/transferOrderPage")
    public String transferOrderPage(Model model,String status,@RequestParam("ids") String ids,@RequestParam("type") String type) {
        List<ProductOrder> list = new ArrayList<>();
        if ("transferOrder".equals(type)){
            String[] strings = ids.split(",");
            for (String string : strings) {
                ProductOrder productOrder = productOrderService.getProductOrderById(Long.valueOf(string));
                list.add(productOrder);
            }
        }
        model.addAttribute("list", list);
        model.addAttribute("count", list.size());
        return "zzsc/receiveOrder/transferOrderPage";
    }
    /**
     * 移交弹窗页面
     * @param model
     * @param status 状态
     * @param timeStatus
     * @return
     */
    @NLog
    @RequestMapping(value = "/devolveonOrderPage")
    public String fileBindingPage(Model model,String status) {
        return "zzsc/receiveOrder/devolveonOrderPage";
    }
    /**
     * json查询所有待接收列表
     * @param params
     * @param request
     * @returnSC_GENERAL_STATE_SCPROORDER_CODE1
     */
    @NLog
    @RequestMapping(value = "/json")
    @ResponseBody
    public DataTableResponse json(@RequestParam Map params, HttpServletRequest request) {
    	DataTableRequest r = DataTableUtils.getParam(request);
        params.put("page", r.getPage());
        params.put("limit", r.getLength());
    	if(null==params.get("statusArr")){
    		params.put("statusArr","all");
    	}
        Map fileMap = new HashMap();
        fileMap.put("signTime", "signTime");
        fileMap.put("allotDate", "allotDate");
        OrderHandleUtils.getOrderCol(r, params, fileMap);

    	List<Map> list = receiveService.findAllWithPage(params);
        String[] transColum = {"originCode"};
        list = translateUtil.translateList(transColum, list);
        String[] hideColum = {"customerPhone"};//隐藏
        list = desUtil.decryptListMap(hideColum, list);
        CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
        int count = Integer.parseInt(params.get("count").toString());//总条数
        return new DataTableResponse(r.getDraw(), count, count, list, "");
    }

    
    /**
     * 接收订单
     *
     * @param scProductOrderListJson
     * @return
     */
    @NLog
    @RequestMapping(value = "/received_order", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse receivedOrder(@RequestParam Map params) {
    	String result =	receiveService.receivedOrder(params);
    	if(result.equals("success")) 
    		 return this.getSuccessResponse("接收成功,当前订单已进入办理中");
    		else
    			return this.getFailResponse(result);
    }
    
    /**
     * 订单退回
     *
     * @param params
     * @return
     */
    @NLog
    @RequestMapping(value = "/go_back", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse goBack(@RequestParam Map params) {
    	ValidateUtils.strNotEmpty(params.get("return_order_remark"),ProductOrderExcption.class,"请输入退回备注");
    	String result =	receiveService.goBack(params);
    	if(result.equals("success")) 
    		 return this.getSuccessResponse("退单成功");
    		else
    			return this.getFailResponse(result);
    }
    
    /**
     * 订单转单
     *
     * @param scProductOrderListJson
     * @return
     */
    @NLog
    @RequestMapping(value = "/transfer_order", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse transferOrder(@RequestParam Map params) {
    	ValidateUtils.strNotEmpty(params.get("transferUserId"),ProductOrderExcption.class,"请选择需要转单的对象");
    	ValidateUtils.strNotEmpty(params.get("transfer_order_remark"),ProductOrderExcption.class,"请输入转单备注");
    	String result =	receiveService.transferOrder(params);
    	if(result.equals("success")) 
    		return this.getSuccessResponse("转单成功");
    	else
    		return this.getFailResponse(result);
    }
    
    /**
     * 订单移交
     *
     * @param scProductOrderListJson
     * @return
     */
    @NLog
    @AuthOpt(code="ZZSC_WAIT_RECEIVE_DEVOLVEON")
    @RequestMapping(value = "/devolveon_order", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse devolveonOrder(@RequestParam Map params) {
    	ValidateUtils.strNotEmpty(params.get("devolveonUserId"),ProductOrderExcption.class,"请选择需要移交的对象");
    	ValidateUtils.strNotEmpty(params.get("devolveon_order_remark"),ProductOrderExcption.class,"请输入移交备注");
    	String result =	receiveService.devolveonOrder(params);
    	if(result.equals("success")) 
    		return this.getSuccessResponse("退单成功");
    	else
    		return this.getFailResponse(result);
    }
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ResponseBody
    public void POIExcel(@RequestParam Map params, HttpServletRequest request, HttpServletResponse response) {
        // 查询数据集合
    	List<Map> list = receiveService.findAllWithPage(params);
        String[] transColum = {"originCode"};
        list = translateUtil.translateList(transColum, list);
        String fileName = "待接收" + ".xls";
        String[] title = {"订单来源", "订单编号", "生产订单编号", "客户", "客户号码", "业务类型", "订单产品", "签单时间","签单商务","签单部门","合同金额","分配人","分配时间","来源方式"};
        String[] key = {"signOrgName", "orderNo", "scProductOrderNo", "customerName", "customerPhone", "businessProductName", "productName", "signTime","signUserName","signUserOrgName","money","allotUserName","allotDate","originCode"};
        ExcelUtil.createN(fileName, title, key, list, response);
    }
    
}
